// Package handler
// @File: user_handler.go
// @Description:
// @Author: YaoRuiQi
// @Date: 2025/8/23 03:00

package handler

import (
	"GinStandardTemplate/internal/dto"
	"GinStandardTemplate/internal/pkg/codes"
	"GinStandardTemplate/internal/pkg/contextUtils"
	"GinStandardTemplate/internal/pkg/response"
	"GinStandardTemplate/internal/service"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type UserHandler struct {
	service *service.UserService
}

func NewUserHandler(s *service.UserService) *UserHandler {
	return &UserHandler{
		service: s,
	}
}

func (h *UserHandler) Login(c *gin.Context) {
	ctx, _ := contextUtils.FromContext(c.Request.Context())
	var data dto.UserLoginRequest
	if err := c.ShouldBindJSON(&data); err != nil {
		ctx.Logger.Error("参数错误", zap.Error(err))
		response.JSONResponse(c, codes.InvalidParam.Code(), err.Error(), nil)
		return
	}
	ctx.Logger.Debug("请求参数", zap.Any("data", data))
	result, err := h.service.Login(ctx, data.Username, data.Password)
	if err != nil {
		response.JSONResponse(c, err.Code.Code(), err.Error(), nil)
	} else {
		response.JSONResponse(c, codes.Success.Code(), codes.Success.Message(ctx, nil), result)
	}
}

func (h *UserHandler) Me(c *gin.Context) {
	ctx, _ := contextUtils.FromContext(c.Request.Context())
	result, err := h.service.Me(ctx)
	if err != nil {
		response.JSONResponse(c, err.Code.Code(), err.Error(), nil)
	} else {
		response.JSONResponse(c, codes.Success.Code(), codes.Success.Message(ctx, nil), result)
	}
}
